//
//  KLAddressListViewController.m
//  used
//
//  Created by xalo on 16/7/23.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "KLAddressListViewController.h"
#import "KLAddressHeaderFooterView.h"
#import "CityCell.h"
@interface KLAddressListViewController ()<UITableViewDataSource, UITableViewDelegate, KLAddressHeaderFooterViewDelegate>

@property (nonatomic, strong) NSMutableArray *citys;

@property (nonatomic, strong) NSMutableArray *provinces;

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic) NSInteger index;


@end

@implementation KLAddressListViewController

- (NSMutableArray *)provinces {
    if (!_provinces) {
        _provinces = [NSMutableArray array];
    }
    return _provinces;
}

- (NSMutableArray *)citys {
    if (!_citys) {
        _citys = [NSMutableArray array];
    }
    return _citys;
}

-(void)showOrRowStateWith:(NSInteger)index{
    if (_index == index) {
        //点击相同的section时，赋一个在section之外的值，让列表不被展开
        _index = 100;
    }else{
        _index = index;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"城市信息";
    [self loadAddressData];
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([CityCell class]) bundle:nil] forCellReuseIdentifier:@"CityCell"];
    self.index = -1;
    
}


- (void)loadAddressData {
    
    // 获取城市菜单
    self.citys = [NSMutableArray array];
    self.provinces = [NSMutableArray array];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"SearchLocations.plist" ofType:nil];
    NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:filePath];
    
    for (NSDictionary *dictArray in dictRoot[@"address"]) {
        
        [self.provinces addObject:dictArray[@"name"]];
        
        NSMutableArray *cityArray = [NSMutableArray array];
        for (NSDictionary *cityDict in dictArray[@"sub"]) {
            [cityArray addObject:cityDict[@"name"]];
        }
        [self.citys addObject:cityArray];
    }

}
//组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.provinces.count;
}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == _index) {
        NSArray *citys = self.citys[section];
        return citys.count;
    }
    
    return 0;
}

//设置sectionHeaderView的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 31;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CityCell"];
    cell.cityName.text = self.citys[indexPath.section][indexPath.row];

    return cell;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    KLAddressHeaderFooterView *secHeaderView = [KLAddressHeaderFooterView headerViewWithTableView:tableView];
    secHeaderView.contentView.backgroundColor = [UIColor whiteColor];
    
    secHeaderView.cityName = self.provinces[section];
    secHeaderView.delegate = self;
    secHeaderView.index = section;
    //点击的时候刷新表视图
    secHeaderView.headerViewClick = ^{
        [tableView reloadData];
    };
    
    return secHeaderView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *arr = self.citys[indexPath.section];
 
    if (self.block) {
        self.block(self.provinces[indexPath.section], arr[indexPath.row]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}



@end